<template>
  <div class="dish-type-management-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span class="title">菜品类型管理</span>
          <el-button type="primary" @click="handleAdd">添加类型</el-button>
        </div>
      </template>

      <!-- 表格区域 -->
      <div class="table-container">
        <table class="dish-type-table">
          <thead>
            <tr>
              <th>编号</th>
              <th>类型名称</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="item in dishTypes" :key="item.dtId">
              <td>{{ item.dtId }}</td>
              <td>{{ item.dtName }}</td>
              <td class="actions-column">
                <div class="action-buttons">
                  <el-button
                    size="small"
                    type="primary"
                    @click="handleEdit(item)"
                    :disabled="item.dtId <= 6"
                  >
                    编辑
                  </el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="handleDelete(item)"
                    :disabled="item.dtId <= 6"
                  >
                    删除
                  </el-button>
                </div>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </el-card>

    <!-- 添加/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '添加菜品类型' : '编辑菜品类型'"
      width="30%"
      @close="resetForm"
    >
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="类型名称" prop="dtName">
          <el-input v-model="form.dtName" placeholder="请输入菜品类型名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance } from 'element-plus'
import { dishTypeApi } from '../api/console'

// 接口类型定义
interface DishType {
  dtId: number
  dtName: string
}

// 状态定义
const loading = ref(false)
const dialogVisible = ref(false)
const dialogType = ref<'add' | 'edit'>('add')
const dishTypes = ref<DishType[]>([])
const formRef = ref<FormInstance>()

// 表单数据
const form = ref({
  dtId: 0,
  dtName: ''
})

// 表单验证规则
const rules = {
  dtName: [
    { required: true, message: '请输入类型名称', trigger: 'blur' },
    { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
  ]
}

// 获取菜品类型列表
const fetchDishTypes = async () => {
  try {
    loading.value = true
    const response = await dishTypeApi.getDishTypes()
    if (response.code === 200) {
      dishTypes.value = response.data
    } else {
      ElMessage.error(response.message || '获取菜品类型列表失败')
    }
  } catch (error) {
    ElMessage.error('获取菜品类型列表失败')
    console.error(error)
  } finally {
    loading.value = false
  }
}

// 添加按钮处理
const handleAdd = () => {
  dialogType.value = 'add'
  dialogVisible.value = true
}

// 编辑按钮处理
const handleEdit = (row: DishType) => {
  dialogType.value = 'edit'
  form.value = { ...row }
  dialogVisible.value = true
}

// 删除按钮处理
const handleDelete = (row: DishType) => {
  ElMessageBox.confirm(
    `确定要删除菜品类型"${row.dtName}"吗？`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(async () => {
    try {
      const response = await dishTypeApi.deleteDishType(row.dtId)
      if (response.code === 200) {
        ElMessage.success('删除成功')
        await fetchDishTypes()
      } else {
        ElMessage.error(response.message || '删除失败')
      }
    } catch (error) {
      ElMessage.error('删除失败')
      console.error(error)
    }
  }).catch(() => {
    // 取消删除操作
  })
}

// 表单提交处理
const handleSubmit = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        const response = dialogType.value === 'add'
          ? await dishTypeApi.addDishType({ dtName: form.value.dtName })
          : await dishTypeApi.updateDishType(form.value)
        
        if (response.code === 200) {
          ElMessage.success(dialogType.value === 'add' ? '添加成功' : '更新成功')
          dialogVisible.value = false
          await fetchDishTypes()
        } else {
          ElMessage.error(response.message || (dialogType.value === 'add' ? '添加失败' : '更新失败'))
        }
      } catch (error) {
        ElMessage.error(dialogType.value === 'add' ? '添加失败' : '更新失败')
        console.error(error)
      }
    }
  })
}

// 重置表单
const resetForm = () => {
  if (formRef.value) {
    formRef.value.resetFields()
  }
  form.value = {
    dtId: 0,
    dtName: ''
  }
}

// 页面加载时获取数据
onMounted(() => {
  fetchDishTypes()
})
</script>

<style scoped>
.dish-type-management-container {
  margin-left: 200px;
  margin-top: 45px;
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.table-container {
  width: 100%;
  overflow-x: auto;
}

.dish-type-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 20px;
  min-width: 600px;
}

.dish-type-table th,
.dish-type-table td {
  border: 1px solid #ddd;
  padding: 12px;
  text-align: center;
  color: #333;
  vertical-align: middle;
}

.dish-type-table th {
  background-color: #f2f2f2;
  font-weight: bold;
  color: #333;
}

.dish-type-table tbody tr:nth-child(even) {
  background-color: #f9f9f9;
}

.dish-type-table tbody tr:hover {
  background-color: #f5f5f5;
}

.actions-column {
  white-space: nowrap;
  display: flex;
  justify-content: center;
  align-items: center;
  min-width: 200px;
}

.action-buttons {
  display: flex;
  gap: 8px;
  justify-content: center;
  width: 100%;
}

.action-buttons .el-button {
  display: flex;
  align-items: center;
  gap: 4px;
  margin: 0;
}

/* 响应式布局 */
@media screen and (max-width: 768px) {
  .dish-type-management-container {
    margin-left: 0;
    width: 100%;
    padding: 10px;
  }

  .card-header {
    flex-direction: column;
    gap: 16px;
  }

  .title {
    width: 100%;
    text-align: center;
  }

  .el-button {
    width: 100%;
    justify-content: center;
  }
}
</style> 